<?php
/**
 *  代课
 */

class Controller_adjust extends MyApp {

	public function __construct()
	{

		parent::__construct();

		$this->model->syllabus = YOU::loadModel('syllabus');
		$this->model->grade    = YOU::loadModel('grade');
		$this->model->class    = YOU::loadModel('class');
		$this->model->subject  = YOU::loadModel('subject');
		$this->model->staff    = YOU::loadModel('staff');
		$this->model->semester = YOU::loadModel('semester');
		$this->model->log      = YOU::loadModel('log');

	}


	/** 首页 **/
	public function actionIndex()
	{

		$this->tpl->display();
	}


	/** 添加代课 **/
	public function actionAdd()
	{
		// 系统日记
		$a = $_GET['a'];
		$log_name = $this->user['user_name'];
		$this->model->log->add($a,$log_name);
		
		// 最新学期
		$semester_data = $this->model->semester->getNew();
		$semester_id   = $semester_data['semester_id'];

		$staff   = $this->model->staff->getIni();
		$class   = $this->model->class->getIni();
		$subject = $this->model->subject->getIni();

		if ($_POST){

			// 原科任老师
			$from_staff     = str_replace("staff_", "", $this->post->from_staff);
			$from_staffname = $staff[$from_staff];


			// 新科任
			$to_staff     = $this->post->to_staff;
			$row          = $this->model->staff->getInfo($to_staff);
			$to_staffname = $row['staff_name'];

			// 启用时间
			$start_time = $this->post->start_time;

			// 学月和周
			$row = $this->model->semester->getWeekByTime($start_time);
			$adjust_smonth = $row['semester_month'];
			$adjust_sweek  = $row['week_number'];

			// 原科任班级和学科
			$pids = $this->post->pids;
            //要转交的课程  是否与  新科任的课程冲突
            foreach ($pids as $key => $value) {
                $tmp = explode("_", $value);

                $from_class = trim($tmp[0]);
                $from_subject = trim($tmp[1]);

                $old_datas = $this->db->fetchAll("SELECT * FROM `tbl_syllabus` WHERE staff_id = '{$from_staff}' AND semester_id='{$semester_id}' AND class_id = '{$from_class}' AND subject_id = '{$from_subject}' AND start_time>'1970-01-01'");
                $olds = array();
                foreach ($old_datas as $key => $value){
                    $olds[] = "syllabus_week='{$value['syllabus_week']}' AND syllabus_section='{$value['syllabus_section']}'";
                }
                foreach ($olds as $key => $value){
                    $new = $this->db->fetchRow("SELECT syllabus_id FROM `tbl_syllabus` WHERE {$value} AND staff_id = '{$to_staff}' AND semester_id='{$semester_id}' AND start_time>'1970-01-01'");
                    if($new){
                        //var_dump($new);exit;
                        Helper_Client::alert("新任课老师的课程表冲突！");
                    }
                }

            }

			foreach ($pids as $key => $value) {
                $tmp = explode("_", $value);

                $from_class = trim($tmp[0]);
                $from_subject = trim($tmp[1]);

                $from_classname = $class[$from_class];
                $from_subjectname = $subject[$from_subject];


				$info = array(
				"semester_id"      => $semester_id,
				"from_class"       => $from_class,
				"from_classname"   => $from_classname,
				"from_subject"     => $from_subject,
				"from_subjectname" => $from_subjectname,
				"from_staff"       => $from_staff,
				"from_staffname"   => $from_staffname,
				"to_staff"         => $to_staff,
				"to_staffname"     => $to_staffname,
				"start_time"       => $start_time,
				"end_time"         => $end_time,
				"adjust_smonth"    => $adjust_smonth,
				"adjust_sweek"     => $adjust_sweek,
				"status"           => 1,
				"create_time"      => date("Y-m-d H:i:s"),
				"update_time"      => "0000-00-00 00:00:00"
				);

				// 添加到数据库
				$this->db->do_insert("tbl_adjust", $info);
                //教师带的课
                $info = array(
                    "staff_id"         => $to_staff,
                    "update_time"      => date("Y-m-d H:i:s"),
                );

                $this->db->do_update("tbl_staff_subject", $info, "class_id='{$from_class}' AND semester_id='{$semester_id}' AND subject_id='{$from_subject}'");
                //课程表修改
                $old_datas = $this->db->fetchAll("SELECT * FROM `tbl_syllabus` WHERE staff_id = '{$from_staff}' AND semester_id='{$semester_id}' AND class_id = '{$from_class}' AND subject_id = '{$from_subject}' AND start_time>'1970-01-01'");
                if(is_array($old_datas)){
                    foreach ($old_datas as $key => $value){
                        $info = array(
                            "staff_id"         => $to_staff,
                            "update_time"      => date("Y-m-d H:i:s"),
                        );

                    $this->db->do_update("tbl_syllabus", $info, "syllabus_id='{$value['syllabus_id']}'");

                    }
                }//if is_array
            }



			// 跳转
			Helper_Client::location("添加成功", "index.php?a=adjust.list");

		}


		// 代课教师列表
		$sb_lists = $this->db->fetchAll("SELECT * FROM `tbl_staff_subject` WHERE semester_id='{$semester_id}'");
		foreach ($sb_lists as $key => $value){
			$data_lists[$value['staff_id']]['name'] = $staff[$value['staff_id']];
			$data_lists[$value['staff_id']]['data'][$value['class_id']."_".$value['subject_id']] = $class[$value['class_id']].$subject[$value['subject_id']];
		}



		// 教师列表
		$staff_lists = $this->model->staff->getAll();


		$this->tpl->assign("start_time", date("Y-m-d"));
		$this->tpl->assign("data_lists", $data_lists);
		$this->tpl->assign("staff_lists", $staff_lists);
		$this->tpl->assign("subject_lists", $subject_lists);
		$this->tpl->display("adjust/adjust.add.tpl.php");
	}




	/** 代课明细 **/
	public function actionList()
	{
		// 系统日记
		$a = $_GET['a'];
		$log_name = $this->user['user_name'];
		$this->model->log->add($a,$log_name);
		
		// 分页
		YOU::loadLib('page', "", '0');

		$tmp = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_adjust`");
		$count = $tmp['count'];
		$page_total = ceil($count/PAGE_LIMIT);

		// 当前页
		$p = intval($_GET['p']);
		if ($p == 0){
			$p  = 1;
		}
		if ($p > 1){
			$start = PAGE_LIMIT * ($p-1);
		}else{
			$start = 0;
		}

		$adjust_lists = $this->db->fetchAll("SELECT * FROM `tbl_adjust` ORDER BY adjust_id DESC LIMIT {$start}, ".PAGE_LIMIT."");
		foreach ($adjust_lists as $key => $value){
			// 原老师编号
			$row = $this->model->staff->getinfo($value['from_staff']);
			$adjust_lists[$key]['staff_number'] = $row['staff_number'];
		}

		$page = new Page(array('total' => $count, 'perpage' => PAGE_LIMIT));
		$page->next_page   = '下一页>';
		$page->pre_page    = '<上一页';
		$page->style_path  = "static/page_style";

		$this->tpl->assign("page", $page->show(4, "yahoo"));

		$this->tpl->assign("adjust_lists", $adjust_lists);
		$this->tpl->display("adjust/adjust.list.tpl.php");
	}


	/**  代课汇总 **/
	public function actionStat()
	{

		// 系统日记
		$a = $_GET['a'];
		$log_name = $this->user['user_name'];
		$this->model->log->add($a,$log_name);
		
		$this->tpl->display("adjust/adjust.stat.tpl.php");
	}



}



?>